package com.weigecoder.warehouse.service.impl;

import com.weigecoder.warehouse.constant.WarehouseConstants;
import com.weigecoder.warehouse.entity.Result;
import com.weigecoder.warehouse.entity.User;
import com.weigecoder.warehouse.mapper.RoleMapper;
import com.weigecoder.warehouse.mapper.UserMapper;
import com.weigecoder.warehouse.page.Page;
import com.weigecoder.warehouse.service.UserService;
import com.weigecoder.warehouse.utils.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    //根据用户名查询用户对象的业务方法
    @Override
    public User findUserByCode(String userCode) {
        return userMapper.findUserByCode(userCode);
    }

    /**
     * 查询用户列表 分页查询
     *
     * @param page
     * @param user
     * @return
     */
    @Override
    public Page queryUserPage(Page page, User user) {
        //查询用户总行数
        int userCount = userMapper.selectUserCount(user);
        //分页查询用户
        List<User> userList = userMapper.selectUserPage(page, user);
        //将查询到的总行数和当前页数据组装到Page对象
        page.setTotalNum(userCount);
        page.setResultList(userList);
        return page;
    }

    /**
     * 添加用户
     * @param user
     */
    @Override
    public Result saveUser(User user) {
        //根据用户名查询用户
        User oldUser = userMapper.findUserByCode(user.getUserCode());
        //用户已存在
        if (oldUser != null) {
            return Result.err(Result.CODE_ERR_BUSINESS, "该用户已存在！");
        }
        //用户不存在,对密码加密,添加用户
        String userPwd = DigestUtil.hmacSign(user.getUserPwd());
        user.setUserPwd(userPwd);
        int i = userMapper.saveUser(user);
        if (i > 0) {
            return Result.ok("添加用户成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "添加用户失败！");
    }

    //修改用户状态的业务方法
    @Override
    public Result updateUserState(User user) {
        //根据用户id修改用户状态
        int i = userMapper.updateUserState(user);
        if (i > 0) {
            return Result.ok("启用或禁用成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "启用或禁用失败！");
    }

    //根据用户ID删除用户
    @Transactional
    @Override
    public Result removeUserByIds(List<Integer> userIds) {
        int i = userMapper.setDeleteByUserIds(userIds);
        //同时删除用户角色表中的数据（其实可以不用删）
        roleMapper.delRoleByUserIdList(userIds);
        if (i > 0) {
            return Result.ok("用户删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "用户删除失败！");
    }

    //修改用户昵称的业务方法
    @Override
    public Result updateUserName(User user) {
        //根据用户id修改用户昵称
        int i = userMapper.updateNameById(user);
        if(i>0){
            return Result.ok("用户修改成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "用户修改失败！");
    }


    //重置密码的业务方法
    @Override
    public Result resetPwd(Integer userId) {
        String hmacSignPwd = DigestUtil.hmacSign(WarehouseConstants.RESETPwd_VALUE);
        //根据用户id修改密码
        int i = userMapper.updatePwdById(userId,hmacSignPwd);
        if(i>0) return Result.ok("密码重置成功！");
        return Result.err(Result.CODE_ERR_BUSINESS, "密码重置失败！");
    }

}
